﻿using System;
using System.IO;
using System.Net;
using System.Net.Mime;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Media;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Provider;

namespace OnlineImageInImageView
{
	[Activity(Label = "ImageView Demo", MainLauncher = true, Icon = "@drawable/icon")]
	public class MainActivity : Activity
	{
		protected override void OnCreate(Bundle bundle)
		{
			base.OnCreate(bundle);
			SetContentView(Resource.Layout.Main);

			ImageView image = FindViewById<ImageView>(Resource.Id.imageView);

			var imageBitmap = GetImageBitmapFromUrl("http://xamarin.com/content/images/pages/platform/sketches-screenshot.png");
			image.SetImageBitmap(imageBitmap);
		}

		private Bitmap GetImageBitmapFromUrl(string url)
		{
			Bitmap imageBitmap = null;
			using (var webClient = new WebClient())
			{
				var imageBytes = webClient.DownloadData(url);
				if (imageBytes != null && imageBytes.Length > 0)
				{
					imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
				}
			}

			return imageBitmap;
		}
	}
}